﻿using Library.CustomControl;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The Basic Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234237

namespace Windows8AppsExample
{
    /// <summary>
    /// A basic page that provides characteristics common to most applications.
    /// </summary>
    public sealed partial class TouchAndThumb : Windows8AppsExample.Common.LayoutAwarePage
    {
        const double BUTTON_SIZE = 60; 
        const double BUTTON_FONT = 18; 
        string blockChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!?-+*/%=";
        Color[] colors = { Colors.Red, Colors.Green, Colors.Orange, Colors.Blue, Colors.Purple }; 
        Random rand = new Random(); 

        public TouchAndThumb()
        {
            this.InitializeComponent();
        }

        /// <summary>
        /// Populates the page with content passed during navigation.  Any saved state is also
        /// provided when recreating a page from a prior session.
        /// </summary>
        /// <param name="navigationParameter">The parameter value passed to
        /// <see cref="Frame.Navigate(Type, Object)"/> when this page was initially requested.
        /// </param>
        /// <param name="pageState">A dictionary of state preserved by this page during an earlier
        /// session.  This will be null the first time a page is visited.</param>
        protected override void LoadState(Object navigationParameter, Dictionary<String, Object> pageState)
        {
        }

        /// <summary>
        /// Preserves state associated with this page in case the application is suspended or the
        /// page is discarded from the navigation cache.  Values must conform to the serialization
        /// requirements of <see cref="SuspensionManager.SessionState"/>.
        /// </summary>
        /// <param name="pageState">An empty dictionary to be populated with serializable state.</param>
        protected override void SaveState(Dictionary<String, Object> pageState)
        {
        }

        private void OnGridSizeChanged(object sender, SizeChangedEventArgs args)
        {
            buttonCanvas.Children.Clear();  
            double widthFraction = args.NewSize.Width / (args.NewSize.Width + args.NewSize.Height);         
            int horzCount = (int)(widthFraction * blockChars.Length / 2);         
            int vertCount = (int)(blockChars.Length / 2 - horzCount);         
            int index = 0;  
            double slotWidth = (args.NewSize.Width - BUTTON_SIZE) / horzCount;         
            double slotHeight = (args.NewSize.Height - BUTTON_SIZE) / vertCount + 1;  
            // Across top         
            for (int i = 0; i < horzCount; i++)         
            {             
                Button button = MakeButton(index++);             
                Canvas.SetLeft(button, i * slotWidth);             
                Canvas.SetTop(button, 0);             
                buttonCanvas.Children.Add(button);         
            }  
            // Down right side         
            for (int i = 0; i < vertCount; i++)         
            {             
                Button button = MakeButton(index++);
                Canvas.SetLeft(button, this.ActualWidth - BUTTON_SIZE);
                Canvas.SetTop(button, i * slotHeight);
                buttonCanvas.Children.Add(button);
            }  
            // Across bottom from right
            for (int i = 0; i < horzCount; i++)
            {
                Button button = MakeButton(index++);
                Canvas.SetLeft(button, this.ActualWidth - i * slotWidth - BUTTON_SIZE);
                Canvas.SetTop(button, this.ActualHeight - BUTTON_SIZE);
                buttonCanvas.Children.Add(button);
            }  
            // Up left side
            for (int i = 0; i < vertCount; i++)
            {
                Button button = MakeButton(index++);
                Canvas.SetLeft(button, 0);
                Canvas.SetTop(button, this.ActualHeight - i * slotHeight - BUTTON_SIZE); 
                buttonCanvas.Children.Add(button);
            }
        }

        Button MakeButton(int index)
        {
            Button button = new Button
            {
                Content = blockChars[index].ToString(), 
                Width = BUTTON_SIZE, Height = BUTTON_SIZE, 
                FontSize = BUTTON_FONT,
                Tag = new SolidColorBrush(colors[index % colors.Length]),
            };
            
            button.Click += OnButtonClick;
            return button;
        }

        void OnButtonClick(object sender, RoutedEventArgs e) 
        {
            Button button = sender as Button;  
            Block block = new Block
            {
                Text = button.Content as string,
                Foreground = button.Tag as Brush
            };
            Canvas.SetLeft(block, this.ActualWidth / 2 - 144 * rand.NextDouble());
            Canvas.SetTop(block, this.ActualHeight / 2 - 144 * rand.NextDouble());
            Canvas.SetZIndex(block, Block.ZIndex);
            blockcanvas.Children.Add(block);
        }
    }
}
